package com.semanticweb.webapp.model.ontology;

import org.apache.lucene.document.Document;

import com.semanticweb.webapp.ontology.CIDOC;
import com.semanticweb.webapp.ontology.Ont;
import com.semanticweb.webapp.ontology.Query;
import com.semanticweb.webapp.search.IDocument;

/**
 * Class which represents 'Fotografija' from the chart. Value is photo URL.
 * 
 * @author Lab
 * 
 */
public class Photography extends DefaultAbstractEntity<String> implements IDocument {

    public static final String ONT_IMAGE = "image";

    public Photography(final String id) {
        super(id);
    }

    @Override
    public void toRDF() {
        if (this.value == null) {
            return;
        }

        final Query query = new Query();
        final String[] result = query.select("image").type("image", CIDOC.E38.queryValue()).label("image", this.value)
                .find();

        final String itemURI = Item.formatItemURI(this.id);
        String imageURI;
        if (result == null || result.length == 0) {
            imageURI = Ont.formatURI(ONT_IMAGE);

            Ont.getOntology().createIndividual(CIDOC.E38.getFullValue(), imageURI);
            Ont.getOntology().addLabel(imageURI, this.value);
        } else {
            imageURI = result[0];
        }

        // connect to item
        Ont.getOntology().connectIndividuals(imageURI, CIDOC.P138.getFullValue(), itemURI);
    }

    public void fromRDF() {
        final String itemQueryURI = Item.formatItemQueryURI(this.id);
        final Query q = new Query();
        final String[] result = q.selectLabel().type("image", CIDOC.E38.queryValue())
                .connect("image", CIDOC.P138.queryValue(), itemQueryURI).findLabel("image");
        if (result != null && result.length > 0) {
            this.value = result[0];
        }

    }

    @Override
    public void toIndexDocument(final Document doc) {
        // Nothing to index
    }
}
